package edu.yourl.web;

import org.apache.wicket.markup.html.basic.Label;
import org.apache.wicket.markup.html.form.Form;
import org.apache.wicket.markup.html.form.PasswordTextField;
import org.apache.wicket.markup.html.form.validation.EqualInputValidator;
import org.apache.wicket.model.PropertyModel;
import org.apache.wicket.spring.injection.annot.SpringBean;
import org.apache.wicket.validation.validator.EmailAddressValidator;

import edu.yourl.application.YourlSession;
import edu.yourl.presenter.IProfilePagePresenter;
import edu.yourl.web.components.PropertyTextField;

public class ProfilePage extends BasePage implements IProfilePage{

	private String name;
	private String password;
	private String repassword;
	private String email;
	private String nickname;
	
	@SpringBean
	IProfilePagePresenter presenter;
	
	
	public ProfilePage() {
		presenter.init(this);
		add(new Label("name",this.nickname));
		add(new ProfileForm("profileForm"));
	}
	
	public String getName() {
		return name;
	}

	public String getPassword() {
		return password;
	}

	public String getRepassword() {
		return repassword;
	}

	public String getEmail() {
		return email;
	}
	
	public void setName(String name) {
		this.name = name;
	}

	public void setPassword(String password) {
		this.password = password;
	}

	public void setRepassword(String repassword) {
		this.repassword = repassword;
	}

	public void setEmail(String email) {
		this.email = email;
	}
	
	public void setNickName(String nickname) {
		this.nickname = nickname;
	}
	
	public IProfilePagePresenter getPresenter() {
		return presenter;
	}

	private class ProfileForm extends Form<String> {
		
		private static final long serialVersionUID = 1L;
				
		public ProfileForm(String id) {
			super(id);
			this.setMultiPart(true);
			PasswordTextField password = new PasswordTextField("password", new PropertyModel<String>(ProfilePage.this, "password"));
			this.add(new PropertyTextField<String>("name",ProfilePage.this).setRequired(true));
			PasswordTextField  repassword = new PasswordTextField("repassword",new PropertyModel<String>(ProfilePage.this, "password"));
			this.add(password);
			this.add(repassword);
			this.add(new PropertyTextField<String>("email",ProfilePage.this).setRequired(true).add(EmailAddressValidator.getInstance()));
			
			this.add(new EqualInputValidator(password, repassword));
			
		}
		
		@Override
		protected void onSubmit() {
			presenter.changeUserData(ProfilePage.this);
			info("Su perfil se ha actualizado");
		}
		
		
	}
	
	public YourlSession getYourlSession() {
		return (YourlSession) super.getSession(); 
	}
	
	public void setPresenter(IProfilePagePresenter presenter) {
		this.presenter = presenter;
	}
}
